package com.fengye.webflux.mongo.config;

import com.fengye.webflux.model.Article;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * 很多时候,需要在系统运行之前做一些初始化操作,为了实现这样的需求,Spring Boot 提供了一个方案,即 {@link CommandLineRunner} 接口.
 * <br>
 * 当 Spring 的 ApplicationContext 初始化完成之后,应用程序中存在的所有 CommandLineRunner 都会被执行.
 * @author sky
 * @date 2021年06月01日 2:11 下午
 */
@Component
public class MyCommandLineRunner {

    @Bean
    CommandLineRunner initData(MongoOperations operations){
        return args -> {
            operations.dropCollection(Article.class);
            operations.insert(new Article(UUID.randomUUID().toString(), "title1", "content1", "miao"));
            operations.insert(new Article(UUID.randomUUID().toString(), "title2", "content2", "miao"));
            operations.findAll(Article.class).forEach(System.out::println);
        };
    }
}
